package wechat.rcv.controller;

import com.google.common.base.Objects;
import com.shrcl.engineerprocess.domain.EngineerProjectAssignment;
import com.shrcl.engineerprocess.domain.EngineerProjectAssignmentStatus;
import com.shrcl.engineerprocess.domain.EngineerWorkOrder;
import edu.fudan.mylang.pf.IObjectFactory;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.collections.IteratorUtils;
import org.apache.log4j.Logger;
import org.eclipse.xtext.xbase.lib.Conversions;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import wechat.rcv.controller.ArrayWapper;
import wechat.rcv.controller.WXNotification;

@RestController
@Transactional
@ResponseBody
@RequestMapping("/myassignment")
@SuppressWarnings("all")
public class GetMyAssignment {
  private final static Logger logger = Logger.getLogger(GetMyAssignment.class.getName());
  
  @Autowired
  private IObjectFactory entities;
  
  @RequestMapping(method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
  public ArrayWapper process(final Long workorderId, final Integer page, final Integer pagesize) {
    Object _xblockexpression = null;
    {
      GetMyAssignment.logger.info("=========>GetMyAssignment.processs<============");
      GetMyAssignment.logger.info(("=========>GetMyAssignment.wechatid<============" + workorderId));
      GetMyAssignment.logger.info(("=========>GetMyAssignment.page<============" + page));
      GetMyAssignment.logger.info(("=========>GetMyAssignment.pagesize<============" + pagesize));
      Object _xtrycatchfinallyexpression = null;
      try {
        EngineerWorkOrder _get = this.entities.<EngineerWorkOrder>get(EngineerWorkOrder.class, workorderId);
        final Collection<EngineerProjectAssignment> e = _get.getProjectAssignments();
        int start = (((page).intValue() - 1) * (pagesize).intValue());
        int end = ((page).intValue() * (pagesize).intValue());
        final ArrayWapper ret = new ArrayWapper();
        final Collection<EngineerProjectAssignment> e_part = new ArrayList<EngineerProjectAssignment>();
        for (int i = start; ((i < end) && (i < e.size())); i++) {
          EngineerProjectAssignment _get_1 = ((EngineerProjectAssignment[])Conversions.unwrapArray(e, EngineerProjectAssignment.class))[i];
          e_part.add(((EngineerProjectAssignment) _get_1));
        }
        final Function1<EngineerProjectAssignment, WXNotification> _function = new Function1<EngineerProjectAssignment, WXNotification>() {
          @Override
          public WXNotification apply(final EngineerProjectAssignment a) {
            WXNotification _wXNotification = new WXNotification();
            final Procedure1<WXNotification> _function = new Procedure1<WXNotification>() {
              @Override
              public void apply(final WXNotification it) {
                Long _id = a.getId();
                it.id = _id;
                EngineerWorkOrder _workOrder = a.getWorkOrder();
                String _rCProjectName = _workOrder.getRCProjectName();
                it.title = _rCProjectName;
                SimpleDateFormat _simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date _startDate = a.getStartDate();
                String _format = _simpleDateFormat.format(_startDate);
                String _plus = (_format + " to ");
                SimpleDateFormat _simpleDateFormat_1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date _endDate = a.getEndDate();
                String _format_1 = _simpleDateFormat_1.format(_endDate);
                String _plus_1 = (_plus + _format_1);
                it.date = _plus_1;
                EngineerProjectAssignmentStatus _status = a.getStatus();
                String _plus_2 = ("=========>GetMyAssignment.Status<============" + _status);
                InputOutput.<String>println(_plus_2);
                EngineerProjectAssignmentStatus _status_1 = a.getStatus();
                boolean _equals = Objects.equal(_status_1, EngineerProjectAssignmentStatus.executing);
                if (_equals) {
                  it.isFinished = Boolean.valueOf(false);
                } else {
                  it.isFinished = Boolean.valueOf(true);
                }
              }
            };
            return ObjectExtensions.<WXNotification>operator_doubleArrow(_wXNotification, _function);
          }
        };
        final Iterable<WXNotification> result = IterableExtensions.<EngineerProjectAssignment, WXNotification>map(e_part, _function);
        Iterator<WXNotification> _iterator = result.iterator();
        List _list = IteratorUtils.toList(_iterator);
        ret.result = _list;
        int _size = ret.result.size();
        ret.count = Integer.valueOf(_size);
        InputOutput.<String>println(("*******GetMyAssignment.result.size******" + ret.count));
        InputOutput.<String>println("*******GetMyAssignment.response******");
        return ret;
      } catch (final Throwable _t) {
        if (_t instanceof Exception) {
          final Exception e_1 = (Exception)_t;
          Object _xblockexpression_1 = null;
          {
            e_1.printStackTrace();
            _xblockexpression_1 = null;
          }
          _xtrycatchfinallyexpression = _xblockexpression_1;
        } else {
          throw Exceptions.sneakyThrow(_t);
        }
      }
      _xblockexpression = _xtrycatchfinallyexpression;
    }
    return ((ArrayWapper)_xblockexpression);
  }
}
